package org.inspursc.s0517.health.evaluation.converter;

import org.inspursc.s0517.health.evaluation.dto.SurveyDTO;
import org.inspursc.s0517.health.evaluation.dto.SurveyQuDetailDTO;
import org.inspursc.s0517.health.evaluation.entity.Survey;
import org.springframework.beans.BeanUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

public class SurveyConverter {
    public static Survey converter(SurveyDTO questionDTO) {
        Survey question = new Survey();
        BeanUtils.copyProperties(questionDTO, question);
        return question;
    }

    public static SurveyDTO converter(Survey question) {
        SurveyDTO dto = new SurveyDTO();
        BeanUtils.copyProperties(question, dto);
        String orderStr = question.getCategoryOrder();
        if (orderStr != null) {
            String orderStrs[] = orderStr.split("\\,");
            dto.setCategoryOrder(Arrays.asList(orderStrs));
        }
        return dto;
    }

    public static SurveyQuDetailDTO converterDetail(Survey survey) {
        SurveyQuDetailDTO dto = new SurveyQuDetailDTO();
        BeanUtils.copyProperties(survey, dto);
        String orderStr = survey.getCategoryOrder();
        if(orderStr!=null && !orderStr.isEmpty()) {
            String orderStrs[] = orderStr.split("\\,");
            List<Integer> orderInt=new ArrayList<>();
            for(String str:orderStrs){
                orderInt.add(Integer.parseInt(str));
            }
            dto.setCategoryOrder(orderInt);
        }
        dto.setCreateDate(getNowDateShort(survey.getCreateDate()));
        return dto;
    }

    private  static  String getNowDateShort(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }


}
